package ynu.edu.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;

/**
 * 容错降级控制器
 * 当服务熔断或不可用时提供备用响应
 */
@RestController
@RequestMapping("/fallback")
public class FallbackController {

    private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    /**
     * Provider服务降级响应
     */
    @GetMapping("/provider")
    public ResponseEntity<Map<String, Object>> providerFallback() {
        Map<String, Object> response = new HashMap<>();
        response.put("error", "Provider Service Unavailable");
        response.put("message", "Provider服务当前不可用，请稍后再试");
        response.put("fallback", true);
        response.put("timestamp", LocalDateTime.now().format(FORMATTER));
        response.put("service", "gateway-service");
        response.put("fallbackType", "circuit-breaker");
        response.put("suggestion", "服务正在恢复中，预计1-2分钟后恢复正常");
        
        System.out.println("🔥 熔断器触发 - Provider服务降级响应");
        return ResponseEntity.ok(response);
    }

    /**
     * 简单服务降级响应
     */
    @GetMapping("/simple")
    public ResponseEntity<Map<String, Object>> simpleFallback() {
        Map<String, Object> response = new HashMap<>();
        response.put("error", "Service Temporarily Unavailable");
        response.put("message", "服务暂时不可用");
        response.put("fallback", true);
        response.put("timestamp", LocalDateTime.now().format(FORMATTER));
        response.put("service", "gateway-service");
        
        System.out.println("🔥 服务降级 - 简单降级响应");
        return ResponseEntity.ok(response);
    }

    /**
     * 限流降级响应
     */
    @GetMapping("/rate-limit")
    public ResponseEntity<Map<String, Object>> rateLimitFallback() {
        Map<String, Object> response = new HashMap<>();
        response.put("error", "Rate Limit Exceeded");
        response.put("message", "请求频率过高，请稍后再试");
        response.put("fallback", true);
        response.put("timestamp", LocalDateTime.now().format(FORMATTER));
        response.put("service", "gateway-service");
        response.put("retryAfter", "60 seconds");
        
        System.out.println("🚦 限流触发 - 限流降级响应");
        return ResponseEntity.status(429).body(response);
    }

    /**
     * 超时降级响应
     */
    @GetMapping("/timeout")
    public ResponseEntity<Map<String, Object>> timeoutFallback() {
        Map<String, Object> response = new HashMap<>();
        response.put("error", "Request Timeout");
        response.put("message", "请求超时，服务响应时间过长");
        response.put("fallback", true);
        response.put("timestamp", LocalDateTime.now().format(FORMATTER));
        response.put("service", "gateway-service");
        response.put("suggestion", "请检查网络连接或稍后重试");
        
        System.out.println("⏰ 超时触发 - 超时降级响应");
        return ResponseEntity.status(504).body(response);
    }

    /**
     * 通用错误降级响应
     */
    @GetMapping("/general")
    public ResponseEntity<Map<String, Object>> generalFallback() {
        Map<String, Object> response = new HashMap<>();
        response.put("error", "Service Error");
        response.put("message", "服务出现错误，已启动备用处理");
        response.put("fallback", true);
        response.put("timestamp", LocalDateTime.now().format(FORMATTER));
        response.put("service", "gateway-service");
        response.put("status", "degraded");
        
        System.out.println("⚠️ 通用错误 - 通用降级响应");
        return ResponseEntity.status(503).body(response);
    }
} 